﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using WpfApp1.Controller;
using WpfApp1.Entity;
using WpfApp1.Service.CertificateService.Dto;

namespace WpfApp1.Service.CertificateService
{
    public class CertificateService
    {
        public async Task submitForm()
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
                client.DefaultRequestHeaders.Add("Authorization", FileController.Token);
                CertificateCreateInput input = new CertificateCreateInput
                {
                    DocId = CertificateNouns.DocId,
                    DocType = CertificateNouns.DocType,
                    Company = CertificateNouns.Company,
                    EquipmentName = CertificateNouns.EquipmentName,
                    DeviceName = CertificateNouns.DeviceName,
                    OSName = CertificateNouns.OSName,
                    OSVersion = CertificateNouns.OSVersion,
                    AntiSoftName = CertificateNouns.AntiSoftName,
                    AntiSoftVerison = CertificateNouns.AntiSoftVerison,
                    AntiScanName = CertificateNouns.AntiScanName,
                    AntiScanVerison = CertificateNouns.AntiScanVerison,
                    InspectionDate = CertificateNouns.InspectionDate,
                    InspectorName = CertificateNouns.InspectorName,
                    SignName = CertificateNouns.SignName,
                    VendorName = CertificateNouns.VendorName,
                    Model = CertificateNouns.Model,
                    OSTypeVersion = CertificateNouns.OSTypeVersion,
                    ArchBit = CertificateNouns.ArchBit,
                    ServicePack = CertificateNouns.ServicePack,
                    LatestPatch = CertificateNouns.LatestPatch,
                    ComputerCount = CertificateNouns.ComputerCount,
                    MacAddressFilePath = CertificateNouns.MacAddressFilePath,
                    McafeeFilePath = CertificateNouns.McafeeFilePath,
                    SystemInfoFilePath = CertificateNouns.SystemInfoFilePath,
                    Remark = CertificateNouns.Remark,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };

                // 将对象序列化为 JSON 字符串
                string json = JsonSerializer.Serialize(input);

                // 创建 HttpContent 对象
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json-patch+json");

                // 后端接口的 URL，需要根据实际情况修改
                string apiUrl = FileController.ApiUrl  + "/api/certificate/create";
                
                // 发送 POST 请求
                HttpResponseMessage response = await client.PostAsync(apiUrl, content);

                // 检查响应状态码
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("数据发送成功！");
                }
                else
                {
                    Console.WriteLine($"数据发送失败，状态码: {response.StatusCode}");
                }
            }
        }

        public async Task updateForm(long id)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
                client.DefaultRequestHeaders.Add("Authorization", FileController.Token);

                CertificateUpdateInput input = new CertificateUpdateInput
                {
                    Id = id,
                    DocId = CertificateNouns.DocId,
                    DocType = CertificateNouns.DocType,
                    Company = CertificateNouns.Company,
                    EquipmentName = CertificateNouns.EquipmentName,
                    DeviceName = CertificateNouns.DeviceName,
                    OSName = CertificateNouns.OSName,
                    OSVersion = CertificateNouns.OSVersion,
                    AntiSoftName = CertificateNouns.AntiSoftName,
                    AntiSoftVerison = CertificateNouns.AntiSoftVerison,
                    AntiScanName = CertificateNouns.AntiScanName,
                    AntiScanVerison = CertificateNouns.AntiScanVerison,
                    InspectionDate = CertificateNouns.InspectionDate,
                    InspectorName = CertificateNouns.InspectorName,
                    SignName = CertificateNouns.SignName,
                    VendorName = CertificateNouns.VendorName,
                    Model = CertificateNouns.Model,
                    OSTypeVersion = CertificateNouns.OSTypeVersion,
                    ArchBit = CertificateNouns.ArchBit,
                    ServicePack = CertificateNouns.ServicePack,
                    LatestPatch = CertificateNouns.LatestPatch,
                    ComputerCount = CertificateNouns.ComputerCount,
                    MacAddressFilePath = CertificateNouns.MacAddressFilePath,
                    McafeeFilePath = CertificateNouns.McafeeFilePath,
                    SystemInfoFilePath = CertificateNouns.SystemInfoFilePath,
                    Remark = CertificateNouns.Remark,
                    UpdateTime = DateTime.Now
                };

                // 将对象序列化为 JSON 字符串
                string json = JsonSerializer.Serialize(input);

                // 创建 HttpContent 对象
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json-patch+json");

                // 后端接口的 URL，需要根据实际情况修改
                string apiUrl = FileController.ApiUrl + "/api/certificate/update";

                // 发送 POST 请求
                HttpResponseMessage response = await client.PostAsync(apiUrl, content);

                // 检查响应状态码
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("数据发送成功！");
                }
                else
                {
                    Console.WriteLine($"数据发送失败，状态码: {response.StatusCode}");
                }
            }
        }

        public async Task deleteForm(long Id)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
                client.DefaultRequestHeaders.Add("Authorization", FileController.Token);


                string apiUrl = FileController.ApiUrl + "/api/certificate/delete";

                var requestBody = new
                {
                    id = Id
                };

                string jsonRequestBody = JsonSerializer.Serialize(requestBody);

                // 创建 HttpContent 对象
                StringContent content = new StringContent(jsonRequestBody, Encoding.UTF8, "application/json-patch+json");

                // 发送 POST 请求
                HttpResponseMessage response = await client.PostAsync(apiUrl, content);

                // 检查响应状态码
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("数据发送成功！");
                }
                else
                {
                    Console.WriteLine($"数据发送失败，状态码: {response.StatusCode}");
                }
            }
        }
    }
}
